package com.fyyg.admin.controller.product;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.fyyg.admin.controller.BaseController;
import com.fyyg.admin.controller.TbusiIndexController;
import com.fyyg.common.result.LayuiResultInfo;
import com.fyyg.common.result.ResultInfo;
import com.fyyg.common.util.date.DateUtil;
import com.fyyg.interfaces.TBusiProductService;
import com.fyyg.model.TBusiProduct;
import com.fyyg.model.TSysUser;

/**
 * 商品控制器
 * @author XiaoYan
 *
 */

@Controller
@RequestMapping("/product")
public class TBusiProductController extends BaseController{
	
	final Logger logger = LogManager.getLogger(TbusiIndexController.class);
	
	
	@Reference private TBusiProductService productServer;
	
	/**
	 * 文件上传样板页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/uploadImgPage")
	public String uploadImgPage(HttpServletRequest request,HttpServletResponse response){
		return  "product/uploadimg";
	}
	
	
	/**
	 * 商品列表页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/productlist")
	public String productList(HttpServletRequest request,HttpServletResponse response){
		return "product/productlist";
	}
	/**
	 * 商品列表页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getProductlist")
	@ResponseBody
	public JSON getProductlist(HttpServletRequest request,HttpServletResponse response){
		
		
		String page = request.getParameter("page");
		String limit = request.getParameter("limit");
		
		String starTime = request.getParameter("starTime");
		String endTime = request.getParameter("endTime");
		String pstatus = request.getParameter("pstatus");
		String proTitle = request.getParameter("proTitle");
		
		LayuiResultInfo result = new LayuiResultInfo();
		result.setCode(1);
		result.setMsg("添加失败");
		
		if(StringUtils.isBlank(page)) {
			page = "1";
		}
		if(StringUtils.isBlank(limit)) {
			limit = "20";
		}
		int index = (Integer.parseInt(page)-1)*Integer.parseInt(limit);
		Map<String, Object> parMap = new HashMap<String, Object>();
		try {
			parMap.put("pageIndex",index);
			parMap.put("pageSize", Integer.parseInt(limit));
			
			parMap.put("starTime", starTime);
			parMap.put("endTime", endTime);
			parMap.put("pstatus", pstatus);
			parMap.put("proTitle", proTitle);
			
			List<TBusiProduct> productList = productServer.queryTbusiProductByProduct(parMap);
			int proCount = productServer.queryTbusiProductByProductCount(parMap);
			
			result.setCode(0);
			result.setMsg("查找成功");
			result.setCount(proCount);
			result.setData(productList);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.ConversionJSON(result);
	}
	

	/**
	 * 添加商品页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/productaddpage")
	public String productAddpPage(HttpServletRequest request,HttpServletResponse response){
		return  "product/productadd";
	}
	
	/**
	 * 添加商品
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping("/productadd")
    @ResponseBody
	public String productAdd(HttpServletRequest request,HttpServletResponse response){
		String UID = (String)request.getSession().getAttribute("UID");
		ResultInfo result = new ResultInfo();
    	result.setCode("error");
    	TSysUser user = this.getCurrentUser(UID,request);
		try {
			String  bannerimage = request.getParameter("banner");
			String  proTitle = request.getParameter("proTitle");
			String  bannerDesc = request.getParameter("bannerDesc");
			String  proPrice = request.getParameter("proPrice");
			String  proDiscountPrice = request.getParameter("proDiscountPrice");
			String  proSellNum = request.getParameter("proSellNum");
			String  proColor = request.getParameter("proColor");
			String  proSize = request.getParameter("proSize");
			String  proSizeDesc = request.getParameter("proSizeDesc");
			String  proEventEndTime = request.getParameter("proEventEndTime");
			String  proDesc = request.getParameter("proDesc");
			if(StringUtils.isBlank(bannerimage)) {
				result.setMessage("请上传商品图片");
			}else if(StringUtils.isBlank(proTitle)) {
				result.setMessage("请输入商品标题");
			}else if(StringUtils.isBlank(bannerDesc)) {
				result.setMessage("请编辑购买按钮描述");
			}else if(StringUtils.isBlank(proPrice)) {
				result.setMessage("请输入商品原价格");
			}else if(StringUtils.isBlank(proDiscountPrice)) {
				result.setMessage("请输入商品出售价格");
			}else if(StringUtils.isBlank(proSellNum)) {
				result.setMessage("请输入商品已售数量");
			}else if(StringUtils.isBlank(proDesc)) {
				result.setMessage("请编辑商品详情");
			}else {
				TBusiProduct product = new TBusiProduct();
				product.setProBannerImage(bannerimage);
				product.setProTitle(proTitle);
				product.setBannerDesc(bannerDesc);
				product.setProPrice(new BigDecimal(proPrice));
				product.setProDiscountPrice(new BigDecimal(proDiscountPrice));
				product.setProSellNum(Integer.parseInt(proSellNum));
				if(!StringUtils.isBlank(proColor)) {
					product.setProColor(proColor);
				}
				if(!StringUtils.isBlank(proSize)) {
					product.setProSize(proSize);
				}
				if(!StringUtils.isBlank(proSizeDesc)) {
					product.setProSizeDesc(proSizeDesc);
				}
				if(!StringUtils.isBlank(proEventEndTime)) {
					product.setProEventEndTime(DateUtil.strToDate(proEventEndTime));
				}
				product.setProDesc(proDesc);
				product.setProStatus("0");
				product.setCreatedBy("21");
				product.setCreatedTime(new Date());
				product.setBannerDesc("验货满意后付款");
				int status = productServer.insert(product);
				if(status>0) {
    				result.setCode("success");
    	    		result.setMessage("添加成功");
    			}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			result.setMessage("服务器异常，请稍后再试");
			logger.error("服务器异常");
        }finally {
        	user=null;
		}
		return result.ConversionJSON(result);
	}
	
	
	
	/**
	 * 获取所有商品
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping("/getproductlistByStatus")
    @ResponseBody
	public String getproductlistByStatus(HttpServletRequest request,HttpServletResponse response){
		String proStatus = request.getParameter("proStatus");
		ResultInfo result = new ResultInfo();
		result.setCode("error");
		result.setMessage("查找失败");
		try {
	    	
	    	List<TBusiProduct> productListByStatus = productServer.queryTbusiProductByStatus(proStatus);
	    	if(productListByStatus.size()>0) {
	    		result.setCode("succ");
	    		result.setMessage("查找成功");
	    		result.setData(productListByStatus);
	    	}
		} catch (RuntimeException e) {
			e.printStackTrace();
			result.setMessage("系统异常，稍后再试");
        }finally {
		}
		return result.ConversionJSON(result);
	}
	
	
	
	

}
